package com.qc31.monitorlib.login

import com.qc31.baselib.R
import com.qc31.baselib.net.AppContext
import com.qc31.baselib.utils.CacheMMKV
import com.qc31.baselib.utils.Callback
import com.qc31.comlib.AppConfigs
import com.qc31.comlib.Keys
import com.qc31.comlib.UrlUtil
import com.qc31.comlib.UserConstant
import com.qc31.comlib.alarm.AlarmDataUtil
import com.qc31.comlib.entity.LoginEntity
import com.qc31.comlib.net.Net
import com.qc31.comlib.choose.car.ChooseCars
import com.qc31.comlib.net.ServiceHelper
import kotlin.concurrent.thread

/** @author joker @date 2022/8/27$ 17:46$ */
object LoginUtil {

    /**
     * Login
     *
     * @param ip
     * @param port
     * @param userId
     * @param pwdMd5
     */
    fun login(ip: String, port: String, userId: String, pwdMd5: String, callback: ILoginCallback) {
        if (!isOkParams(ip, port, userId, pwdMd5, callback)) return
        //清除所有用户数据
        if (isDelete(ip, port, userId)) {
            ChooseCars.clearChooseCar()
            CacheMMKV.clearDefAll()
        }
        UserConstant.current_ip = ip
        UserConstant.current_port = port
        Net.changeUrl()
        ServiceHelper.getRepository().changeHost()
        ServiceHelper.getRepository().login(userId, pwdMd5, "user").subscribe({
            UserConstant.sessionId = it.sessionId
            UserConstant.isRealVideo = it.menuIds.contains("106")
            UserConstant.isTapeVideo = it.menuIds.contains("107")
            thread {
                getAppConfig(callback)
            }
            thread {
                saveData(userId, pwdMd5)
                setUserConstant(it)
                ServiceHelper.getRepository().changeVideoHost()
                AlarmDataUtil.instance.init()
            }

        }, {
            callback.onFailure(it.message ?: "")
        })
    }

    /** 保存数据 */
    private fun saveData(userId: String, pwdMd5: String) {
        CacheMMKV.encodeSave(Keys.LOGIN_TYPE_USERID, userId)
        CacheMMKV.encodeSave(Keys.LOGIN_TYPE_PWD, pwdMd5)
        UserConstant.current_pwd_md5 = pwdMd5
        UserConstant.current_userId = userId
    }

    /** 设置用户相关常量参数 */
    private fun setUserConstant(entity: LoginEntity) {
        UserConstant.current_carId = entity.carId
        UserConstant.current_cmpId = entity.cmpId
        UserConstant.current_car_num = entity.carNum
        UserConstant.current_videos = entity.videos
        UserConstant.adLink_1 = entity.appAdLink1
        UserConstant.adLink_2 = entity.appAdLink2
        UserConstant.adLink_3 = entity.appAdLink3
        AppConfigs.isPlateColorVfy = entity.pwdConvert ?: false
        AppConfigs.isUpPwd = entity.uptPwd ?: false

        UrlUtil.setVideoHost(entity.videoHost)
        CacheMMKV.encode(Keys.CMP_ID, entity.cmpId)

    }

    /** 获取APP自定义设置 */
    private fun getAppConfig(callback: ILoginCallback) {
        ServiceHelper.getRepository().getAppConfig().subscribe({
            AppConfigs.appGpsColumn = it.appGpsColumn
            AppConfigs.mapMark = it.mapMarker
            Callback.mapMarkerSub.onNext(true)
            LocationStatusModel.initData()
            callback.onSuccess()
        }, {
            Callback.mapMarkerSub.onNext(false)
            LocationStatusModel.initData()
            callback.onSuccess()
        })
    }

    private fun isOkParams(
        ip: String,
        port: String,
        userId: String,
        pwdMd5: String,
        callback: ILoginCallback
    ): Boolean {
        if (ip.isEmpty()) {
            callback.onFailure(AppContext.getDescById(R.string.hint_toast_platform_ip))
            return false
        }
        if (port.isEmpty()) {
            callback.onFailure(AppContext.getDescById(R.string.hint_toast_platform_port))
            return false
        }
        if (userId.isEmpty()) {
            callback.onFailure(AppContext.getDescById(R.string.hint_toast_user_opt))
            return false
        }
        if (pwdMd5.isEmpty()) {
            callback.onFailure(AppContext.getDescById(R.string.toast_platform_host_port))
            return false
        }
        return true
    }


    /** 是否删除原账号的数据 */
    private fun isDelete(ip: String, port: String, userId: String): Boolean {
        return CacheMMKV.decodeStringSave(Keys.LOGIN_TYPE_IP) != ip
                || CacheMMKV.decodeStringSave(Keys.LOGIN_TYPE_PORT) != port
                || CacheMMKV.decodeStringSave(Keys.LOGIN_TYPE_USERID) != userId
    }

}